<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用继承增强内置类</title>
</head>
<body>
    <script>
        // function Arr(...args){
        //     args.forEach(item => this.push(item));
        //     this.first = function(){
        //         return this[0];
        //     }
        //     this.max = function(){
        //         return this.sort((a,b) => b-a)[0];
        //     }
        // }
        // Arr.prototype = Object.create(Array.prototype);
        // let hd = new Arr(1,2,3,4,5,6,7,8);
        // console.log(hd);
        // console.log(hd.first());
        // console.log(hd.max());


        class Arr extends Array{
            constructor(...args){
                super(...args);
            }

            first(){
                return this[0];
            }

            max(){
                return this.sort((a,b) => b-a)[0];
            }

            add(item){
                this.push(item);
            }

            remove(value){
                let idx = this.findIndex(item => item===value);
                this.splice(idx,1);
            }
        }

        let hd = new Arr(1,2,3,4,5,6,7,8);
        hd.add(99);
        hd.remove(8);
        console.log(hd);
        console.log(hd.first());
        console.log(hd.max());
    </script>
</body>
</html>